/**
 * @file subeventlist.c
 * @author ken (kangjiening@openvalley.net)
 * @brief 时间订阅链表
 * @version 0.1
 * @date 2023-06-14
 * 
 * @copyright Copyright (c) 2023 湖南开鸿智谷数字产业有限公司
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-06-14 <td>v1.0     <td>ken     <td>内容
 * </table>
 */


#include "os_list.h"
#include "subeventlist.h"


struct SubListId_S * SubEventListInit(void)
{
    struct SubListId_S *subListId = Event_Malloc(sizeof(struct SubListId_S));
    if(subListId == NULL) return NULL;
    subListId->metexId =  MUTEX_InitValue();
    OS_ListInit(&subListId->subListHead);
   return subListId;
}

int  SubEventListDestroy(struct SubListId_S *subListId)
{
    struct SubListId_S *subListId = Event_Malloc(sizeof(struct SubListId_S));
    if(subListId == NULL) return NULL;
    MUTEX_Delete(subListId->metexId);
    Event_free(subListId);
   return subListId;
}

int SubEventListInsert(SubListId_S * subListId,struct InnerEvent_S *innerEvent)
{
    DL_LIST *pList = NULL;
    struct SubList_S *pSubList = NULL;
    int re = -1;
    if (OS_ListEmpty(&subListId->subListHead))
        return re;
    MUTEX_Lock(MutexId mutex);
    OS_DL_LIST_FOR_EACH(pList, &subListId->subListHead)
    {
        pSubList  = OS_DL_LIST_ENTRY(pList, struct SubList_S, subList);
        if (pSubList->event == innerEvent)
        {
            re = 0;
            break;
        }
    }
    
    OS_ListInit(&pSubList->subList);
    OS_ListAdd(&subListId->subListHead, &pSubList->subList);
    MUTEX_Unlock(MutexId mutex);
    return re;
}


int SubEventListDelete(SubListId_S * subListId,struct InnerEvent_S *innerEvent)
{
     DL_LIST *pList = NULL;
    struct SubList_S *pSubList = NULL;
    int re = -1;
    if (OS_ListEmpty(&subListId->subListHead))
        return re;
    MUTEX_Lock(MutexId mutex);
    OS_DL_LIST_FOR_EACH(pList, &subListId->subListHead)
    {
        pSubList  = OS_DL_LIST_ENTRY(pList, struct SubList_S, subList);
        if (pSubList->event == innerEvent)
        {
            
            OS_ListDelInit(pList);   
            Event_free(pSubList);
            
            re = 0;
            break;
        }
    }
    MUTEX_Unlock(MutexId mutex)
    return re;
}

int SubEventListGetByNum(SubListId_S * subListId,uint32_t topicNum,struct InnerEvent_S *innerEvent)
{
     DL_LIST *pList = NULL;
    struct SubList_S *pSubList = NULL;
    int re = -1;
    if (OS_ListEmpty(&subListId->subListHead))
        return re;
    MUTEX_Lock(MutexId mutex);
    OS_DL_LIST_FOR_EACH(pList, &subListId->subListHead)
    {
        pSubList  = OS_DL_LIST_ENTRY(pList, struct SubList_S, subList);
        if ((pSubList->event)->numTopic == topicNum)
        {
            innerEvent = pSubList->event;  
            re = 0;
            break;
        }
    }
    MUTEX_Lock(MutexId mutex);
    return re;
}

int SubEventListGetByString(SubListId_S * subListId,char *topicStr,struct InnerEvent_S *innerEvent)
{
     DL_LIST *pList = NULL;
    struct SubList_S *pSubList = NULL;
    int re = -1;
    if (OS_ListEmpty(&subListId->subListHead))
        return re;
    MUTEX_Lock(MutexId mutex);
    OS_DL_LIST_FOR_EACH(pList, &subListId->subListHead)
    {
        pSubList  = OS_DL_LIST_ENTRY(pList, struct SubList_S, subList);
        if (strcmp((pSubList->event)->strTopic,topicStr) == 0)
        {
            innerEvent = pSubList->event;  
            re = 0;
            break;
        }
    }
    MUTEX_Lock(MutexId mutex);
    return re;
}